package cn.wjssl.domain.activity.service.quota;

import cn.wjssl.domain.activity.model.aggregate.CreateQuotaOrderAggregate;
import cn.wjssl.domain.activity.model.entity.*;
import cn.wjssl.domain.activity.model.valobj.ActivitySkuStockKeyVO;
import cn.wjssl.domain.activity.model.valobj.OrderStateVO;
import cn.wjssl.domain.activity.repository.IActivityRepository;
import cn.wjssl.domain.activity.service.IRaffleActivitySkuStockService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class RaffleActivityAccountQuotaService extends AbstractRaffleActivityAccountQuota implements IRaffleActivitySkuStockService {

    // 定义时间格式, 用于对应数据库中Day, Month
    private final SimpleDateFormat dateFormatMonth = new SimpleDateFormat("yyyy-MM");
    private final SimpleDateFormat dateFormatDay = new SimpleDateFormat("yyyy-MM-dd");

    @Resource
    private IActivityRepository repository;

    // 创建订单聚合对象, 从入参中组合创建订单实体 ActivityOrderEntity, 然后再构建订单聚合对象
    @Override
    protected CreateQuotaOrderAggregate buildOrderAggregate(SkuRechargeEntity skuRechargeEntity, ActivitySkuEntity activitySkuEntity, ActivityEntity activityEntity, ActivityCountEntity activityCountEntity) {
        // 订单实体对象
        ActivityOrderEntity activityOrderEntity = new ActivityOrderEntity();
        activityOrderEntity.setUserId(skuRechargeEntity.getUserId());
        activityOrderEntity.setSku(skuRechargeEntity.getSku());
        activityOrderEntity.setActivityId(activityEntity.getActivityId());
        activityOrderEntity.setActivityName(activityEntity.getActivityName());
        activityOrderEntity.setStrategyId(activityEntity.getStrategyId());
        // 公司里一般会有专门的雪花算法UUID服务，我们这里直接生成个12位就可以了。
        activityOrderEntity.setOrderId(RandomStringUtils.randomNumeric(12));
        activityOrderEntity.setOrderTime(new Date());
        activityOrderEntity.setTotalCount(activityCountEntity.getTotalCount());
        activityOrderEntity.setDayCount(activityCountEntity.getDayCount());
        activityOrderEntity.setMonthCount(activityCountEntity.getMonthCount());
        // 这里只透传订单金额, 通过订单的类型, 调用各自的处理器来处理
        // activityOrderEntity.setState(OrderStateVO.completed);
        activityOrderEntity.setPayAmount(activitySkuEntity.getProductAmount());
        activityOrderEntity.setOutBusinessNo(skuRechargeEntity.getOutBusinessNo());

        // 构建聚合对象
        return CreateQuotaOrderAggregate.builder()
                .userId(skuRechargeEntity.getUserId())
                .activityId(activitySkuEntity.getActivityId())
                .totalCount(activityCountEntity.getTotalCount())
                .dayCount(activityCountEntity.getDayCount())
                .monthCount(activityCountEntity.getMonthCount())
                .activityOrderEntity(activityOrderEntity)
                .build();
    }

    // 弹出sku的redis队列中的第一个, 用于同步消耗数据库库存
    @Override
    public ActivitySkuStockKeyVO takeQueueValue() throws InterruptedException {
        return repository.takeQueueValue();
    }

    @Override
    public ActivitySkuStockKeyVO takeQueueValue(Long sku) throws InterruptedException {
        return repository.takeQueueValue(sku);
    }

    // 同步更新mysql库存
    @Override
    public void updateActivitySkuStock(Long sku) {
        repository.updateActivitySkuStock(sku);
    }

    // 缓存库存以消耗完毕，直接更新数据库库存为0
    @Override
    public void clearActivitySkuStock(Long sku) {
        repository.clearActivitySkuStock(sku);
    }

    // 活动商品sku的redis队列
    @Override
    public void clearQueueValue() {
        repository.clearQueueValue();
    }

    // 获取用户 [当日] 抽奖次数
    @Override
    public Integer queryRaffleActivityAccountDayPartakeCount(Long activityId, String userId) {
        return repository.queryRaffleActivityAccountDayPartakeCount(activityId, userId);
    }

    // 查询用户account总账户
    @Override
    public ActivityAccountEntity queryActivityAccountEntity(Long activityId, String userId) {
        String day = dateFormatDay.format(new Date());
        String month = dateFormatMonth.format(new Date());
        return repository.queryActivityAccountEntity(activityId, userId, day, month);
    }

    // 查询用户 [当前总共] 抽奖次数
    @Override
    public Integer queryRaffleActivityAccountTotalPartakeCount(Long activityId, String userId) {
        return repository.queryRaffleActivityAccountTotalPartakeCount(activityId, userId);
    }

    @Override
    public void updateOrder(DeliveryOrderEntity deliveryOrderEntity) {
        repository.updateOrder(deliveryOrderEntity);
    }

    // 查询所有sku商品的sku编号
    @Override
    public List<Long> querySkuList() {
        return repository.querySkuList();
    }
}
